﻿unit unit1;

interface

uses
     //
     dwBase,

     
     //
     CloneComponents,
     SynCommons,
     //ZAbstractRODataset, ZDataset, ZConnection,ZAbstractDataset, ZAbstractConnection,

     //
     Math,
     Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
     Dialogs, StdCtrls, jpeg, ExtCtrls, DB,  Data.Win.ADODB, Vcl.Imaging.pngimage;

type
  TForm1 = class(TForm)
    Panel_All: TPanel;
    Panel_99_Foot: TPanel;
    Label14: TLabel;
    Label15: TLabel;
    Panel4: TPanel;
    Panel_10_Posts: TPanel;
    Panel_Post: TPanel;
    Panel_00_PostBase: TPanel;
    Label_CreateDate: TLabel;
    Panel_00_Title: TPanel;
    Label1: TLabel;
    Label_ThreadTitle: TLabel;
    StaticText_Poster: TStaticText;
    Panel_Space: TPanel;
    Label_Floor: TLabel;
    ADOQuery: TADOQuery;
    Panel_00_Banner: TPanel;
    Panel_00_Center: TPanel;
    Image_logo: TImage;
    Label_FAQs: TLabel;
    Label_Download: TLabel;
    Label_ContactUs: TLabel;
    Label_gitee: TLabel;
    Label_blog: TLabel;
    Label3: TLabel;
    Label10: TLabel;
    Forum1: TStaticText;
    Forum2: TStaticText;
    StaticText_Home: TStaticText;
    Forum5: TStaticText;
    Forum7: TStaticText;
    Forum4: TStaticText;
    Forum6: TStaticText;
    Forum3: TStaticText;
    Button_Login: TButton;
    Button_User: TButton;
    Button_Logout: TButton;
    Panel_01_Content: TPanel;
    Panel_01_Center: TPanel;
    Panel_00_Blank: TPanel;
    Panel_11_Reply: TPanel;
    Memo_Reply: TMemo;
    Panel1: TPanel;
    Label4: TLabel;
    Label_Message: TLabel;
    Panel2: TPanel;
    Button_Reply: TButton;
    Button_Size: TButton;
    StaticText_Uper: TStaticText;
    procedure FormCreate(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure FormMouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure Button_ReplyClick(Sender: TObject);
    procedure Button_LoginClick(Sender: TObject);
    procedure Button_LogoutClick(Sender: TObject);
    procedure Button_UserClick(Sender: TObject);
    procedure FormStartDock(Sender: TObject; var DragObject: TDragDockObject);
    procedure Button_SizeClick(Sender: TObject);
  private
     giTid : Integer;
     giUid : Integer;
     giUperId : Integer;
     gsCDate : string;
     gsSubject : string;
     gsUper : string;
     //giUserId : Integer;
  public
        function GetUserIDByName(AName:String):Integer;
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}


procedure TForm1.Button_LoginClick(Sender: TObject);
begin
     dwOpenUrl(self,'/bbslogin.dw','_self');

end;

procedure TForm1.Button_LogoutClick(Sender: TObject);
begin
    dwMessageDlg('确定要注销登录吗？','注销','确定','取消','query_logout',self);

end;

function _proc(AText:String):String;
begin
    Result  := StringReplace(AText,'''','''''',[rfReplaceAll]);
end;


procedure TForm1.Button_ReplyClick(Sender: TObject);
var
    sNow    : String;
    iPid    : Integer;
    iPCount : Integer;
begin
    if Button_User.Visible then begin
        //先取得当前日期的PHP值备用
        sNow    := dwDateToPHPDate(Now).ToString;


        //更新bbs_posts.在当前posts表中插入一条记录
        ADOQuery.Close;
        ADOQuery.SQL.Text   := Format('INSERT INTO bbs_post (tid,uid,create_date,message,message_fmt) '
                +'VALUES(%d,%d,%s,''%s'',''%s'')',[giTid,giUId,sNow,_Proc(Memo_Reply.Text),_Proc(Memo_Reply.Text)]);
        ADOQuery.ExecSQL;

        //得到最后的pid。
        ADOQuery.Close;
        ADOQuery.SQL.Text   := 'select [pid] FROM bbs_post WHERE (tid='+giTid.toString+') AND (create_date='''+sNow+''')';
        ADOQuery.Open;
        iPid    := ADOQuery.Fields[0].AsInteger;

        //得到当前主题的post总数
        ADOQuery.Close;
        ADOQuery.SQL.Text   := 'select Count(*) FROM bbs_post WHERE (tid='+giTid.toString+')';
        ADOQuery.Open;
        iPCount := ADOQuery.Fields[0].AsInteger;


        //更新bbs_thread.更新当前主题的最后时间, 回贴数和最后回贴人等信息
        ADOQuery.Close;
        ADOQuery.SQL.Text   := 'UPDATE bbs_thread'
                +' SET last_date = '''+sNow+''', posts = '+iPCount.ToString+',lastpid='+giUId.ToString
                +' WHERE tid='+giTid.ToString;
        ADOQuery.ExecSQL;


        //更新dw_user
        ADOQuery.Close;
        ADOQuery.SQL.Text   := 'UPDATE dw_user SET last_date = '''+sNow+''', posts = posts+1  WHERE uid='+giUId.ToString;
        ADOQuery.ExecSQL;

        //
        dwRunJS('location.reload();',self);

    end else begin
        dwOpenUrl(self,'/bbslogin','_self');
    end;
end;

procedure TForm1.Button_SizeClick(Sender: TObject);
begin
    Memo_Reply.Height   := Memo_Reply.Height + 50;
end;

procedure TForm1.Button_UserClick(Sender: TObject);
var
    iUserId : Integer;
begin
    //取得用户id
    iUserId := GetUserIdByName(Button_User.Caption);
    //
    dwShowMessage('欢迎欢迎！您是第 '+iUserId.ToString+' 位会员！',self);

end;

procedure TForm1.FormCreate(Sender: TObject);
begin
    Top     := 0;
    //
    //giUserId    := -1;
end;

procedure TForm1.FormShow(Sender: TObject);
var
    sParams     : String;
    sUper       : string;
    sSubject    : string;
    sCookie     : string;

    //
    iCreate     : Integer;
    iPos        : Integer;
    iItem       : Integer;

    //
    oPanel      : TPanel;
    joUser      : Variant;

begin

    //读cookie
    try
        sCookie    := dwGetCookie(self,'bbs_user');
        sCookie    := dwAESDecrypt(sCookie,'bbs123');
        if dwStrIsJson(sCookie) then begin
            joUser      := _json(sCookie);

            //
            Button_User.Caption     := joUser.name;
            giUId                   := joUser.uid;
            //显隐控件
            Button_Logout.Left      := 9999;
            Button_User.Visible     := True;
            Button_Logout.Visible   := True;
            BUtton_Login.Visible    := False;
        end;
    except
    end;


     //清空多余
     for iItem := Panel_10_Posts.ControlCount-1 downto 0 do begin
          if TPanel(Panel_10_Posts.Controls[iItem]).Visible then begin
               TPanel(Panel_10_Posts.Controls[iItem]).Destroy;
          end;
     end;


     //tid=294&&create_date=234234&&uid=1324&&subject=还有人学delphi吗。&&uper=kevinlin

     //得到URL参数
     sParams   := dwUnescape(dwGetProp(self,'params'));

     //异常检测
     if sParams = '' then begin
          Exit;
     end;

     //得到各个参数
     //../bbsthread.dw?tid=242&&uid=2&&create_date=1564645898&&subject=delphi%20Format%u683C%u5F0F%u5316%u51FD%u6570&&uper=chengyang

    //tid
    iPos      := Pos('&&',sParams);
    if iPos>0 then begin
        //问题id
        giTid       := StrToIntDef(Copy(sParams,5,iPos-5),-1);
        Delete(sParams,1,iPos+1);
        //uid用户ID
        iPos        := Pos('&&',sParams);
        giUperId    := StrToIntDef(Copy(sParams,5,iPos-5),-1);
        Delete(sParams,1,iPos+1);
        //create_date 时间
        iPos        := Pos('&&',sParams);
        gsCDate     := Copy(sParams,13,iPos-13);
        Delete(sParams,1,iPos+1);
        //subject 问题标题
        iPos      := Pos('&&',sParams);
        gsSubject  := Copy(sParams,9,iPos-9);
        Delete(sParams,1,iPos+1+5);
    end else begin
        giTid   := StrToIntDef(Copy(sParams,5,Length(sParams)-4),-1);
    end;
    //
    sUper     := sParams;

    //更新当前主题的浏览次数 . 浏览次数+1
    ADOQuery.Close;
    ADOQuery.SQL.Text    := 'UPDATE bbs_thread SET views = views + 1 '
            +' WHERE tid='+IntToStr(giTid);
    ADOQuery.ExecSQL;

    //取得发贴者名称
    ADOQuery.Close;
    ADOQuery.SQL.Text    := 'SELECT username FROM dw_user WHERE uid='+giUperId.ToString;
    ADOQuery.Open;
    gsUper  := ADOQuery.Fields[0].AsString;


    //显示当前主题的基本信息
    Label_ThreadTitle.Caption     := gsSubject;
    StaticText_Uper.Caption       := '题主: '+gsUper;
    StaticText_Uper.Hint          := '{"href":"bbsuser?uid='+IntToStr(giUperid)+'"}';
    Label_CreateDate.Caption      := FormatDateTime('yyyy-mm-dd',dwPHPToDate(iCreate));

    //读取post
    ADOQuery.Close;
    ADOQuery.SQL.Text    := 'SELECT a.uid,a.message,a.message_fmt,a.create_date,b.username'
            +' FROM bbs_post a LEFT JOIN dw_user b'
            +' ON a.uid=b.uid'
            +' WHERE a.tid='+IntToStr(giTid)
            +' ORDER BY a.pid';
    ADOQuery.Open;
    //
    for iItem := 0 to ADOQuery.RecordCount-1 do begin
        oPanel    := TPanel(CloneComponent(Panel_Post));
        oPanel.Visible      := True;
        oPanel.Top          := 90000;

        //论坛楼层
        with TLabel(Self.FindComponent('Label_Floor'+IntToStr(iItem+1))) do begin
            Caption   := IntToStr(iItem+1)+' 楼';
        end;

        //uper
        with TStaticText(Self.FindComponent('StaticText_Poster'+IntToStr(iItem+1))) do begin
            Caption := (ADOQuery.FieldByName('username').AsString);
            Hint      := '{"href":"bbs_user.dw?uid='+ADOQuery.FieldByName('uid').AsString+'"}';
        end;

        //消息
        with TLabel(Self.FindComponent('Label_Message'+IntToStr(iItem+1))) do begin
            Caption     := dwLongStr((ADOQuery.FieldByName('message_fmt').AsString));
            //AutoSize  := False;
            AutoSize  := True;
            //AutoSize  := False;
            //Height    := Round(Height*2);
        end;

        //日期
        with TLabel(Self.FindComponent('Label_CreateDate'+IntToStr(iItem+1))) do begin
            Caption   := FormatDateTime('yyyy-mm-dd hh:MM:ss',dwPHPToDate(ADOQuery.FieldByName('create_date').AsInteger));
        end;
        //
        oPanel.AutoSize     := False;
        oPanel.AutoSize     := True;

        //
        ADOQuery.Next;
    end;

    //
    Panel_10_Posts.AutoSize  := False;
    Panel_10_Posts.AutoSize  := True;
    //
    Panel_01_Center.AutoSize    := False;
    Panel_01_Center.AutoSize    := True;
    //
    Panel_01_Content.AutoSize   := False;
    Panel_01_Content.AutoSize   := True;

    //
    Panel_All.AutoSize  := False;
    Panel_All.AutoSize  := True;

    //
    Panel_11_Reply.Top      := 99990;
    Panel_99_Foot.Top       := 99999;
    //
    dwSetHeight(self,Panel_All.Height);
end;

procedure TForm1.FormStartDock(Sender: TObject; var DragObject: TDragDockObject);
var
    sMethod : string;
    sValue  : string;
begin
    sMethod := dwGetProp(Self,'interactionmethod');
    //通过类似以下得到返回的结果，为'1'时表示为"确定"，否则为"取消"
    sValue  := dwGetProp(Self,'interactionvalue');
    //
    if sMethod = 'query_logout' then  begin
        if sValue = '1' then begin
            //清除COOKIE
            dwSetCookie(self,'bbsuser','',0);
            //
            Button_Logout.Visible   := False;
            Button_User.Visible     := False;
            //
            Button_Login.Visible    := True;
            //
            //giUserID    := -1;
        end;
    end;
end;

function TForm1.GetUserIDByName(AName: String): Integer;
begin
    ADOQuery.Close;
    ADOQuery.SQL.Text   := 'SELECT id FROM dw_user WHERE username='''+AName+'''';
    ADOQuery.Open;
    Result  := ADOQuery.Fields[0].AsInteger;
end;

procedure TForm1.FormMouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
     //
     if (X>600)and(Y>600) then begin
        Width     := X-24;
     end else begin
         Width  := 1110;
     end;
end;

end.
